  
	var win;
	var uploadProjectDocStore=CRM.commons.createStore('uploadProjectDocStore',CRM.SPComp.uploadDocDataArray,'projectdocument/docList');
	var docTypeStore=CRM.commons.createComboStore('docTypeStore', 'projectdocument/documentTypes', '');
	
	function createProjectDocumentUploader(projectId){
		
		var uploadDetailsPanel = {
				xtype	: 'fieldset',
				id		: 'uploadDetailsPanel',
				title	: 'Document Details',
				autoHeight: true,
				width	: 435,
				border	: true,
				layout	: 'form',
				items	: [		
				     	   	   {
									xtype	  : 'textfield',
									inputType : 'file',
									id		  : 'file',
									fieldLabel: 'Choose File'
							   },
					     	   {
					     	   		xtype	  : 'datefield',
					     	   		id		  : 'receiveDate',
					     	   		fieldLabel: 'Receiving Date',
					     	   		format   : 'Y-m-d'
					     	   },
					     	   {
					     		   	xtype		  : 'combo',
					     		   	id			  : 'documentType',
					     		   	fieldLabel	  : 'Document Type',
					     		   	value		  : 'SELECT',
					     		   	width		  : 300,
					     		   	store         : docTypeStore,
		   		     		   	 	displayField  : 'criteria',
		   		     		   	 	valueField    : 'id',
		   		     		   	 	triggerAction : 'all',
		   		     		   	 	typeAhead     : true,
		   		     		   	 	editable      : false,
		   		     		        listeners	  : {select : function(){
		   	   						                 bindValue('documentType', 'documentTypeId');
		   	   					                     } 
		   	   			                         }
					     	   },
					     	   {
					     		    xtype	: 'hidden',
					     		    id		: 'documentTypeId'
					     	   },
					     	   {
					     		   	xtype	: 'textfield',
					     		   	id		: 'documentName',
					     		   	fieldLabel: 'Document Name',
					     		   	width	: 300
					     	   }/*,
					     	   {
					     		   	xtype	: 'textfield',
					     		   	id		: 'uploadedBy',
					     		   	fieldLabel: 'Uploaded By',
					     		   	width	: 300
					     	   }*/
					     	   
					     	],
				buttons	 :	[
				       	  	 	{
				       	  	 		xtype	: 'button',
				       	  	 		id		: 'saveButton',
				       	  	 		text	: 'Upload',
				       	  	 		width	: 60,
				       	  	 		handler	: function(){
				       	  	 						uploadProjectDocument(projectId);
				       	  	 					}
				       	  	 	},
				       	  	 	{
				       	  	 		xtype	: 'button',
				       	  	 		id		: 'resetButton',
				       	  	 		text	: 'Reset',
				       	  	 		width	: 60,
				       	  	 		handler	: function(){
				       	  	 						resetFileUploadForm();
				       	  	 					}
				       	  	 	}				       	        
				       		]
		};
		
		var mainUploadProjectPanel = new Ext.FormPanel({
            id          : 'mainUploadProjectPanel',
            autoHeight  : true,
            autoWidth   : 700,
            fileUpload	: true,
            frame		: true,
            items       : [uploadDetailsPanel, 
                           {
            				xtype	: 'panel',
            				title	: 'Uploaded Doc. List',
            				items	: [
            				     	   	CRM.commons.createGrid('projectDocGrid', uploadProjectDocStore, CRM.SPComp.createUploadDocColumnModel())
            				     	   ]
                           }
                           ],
            bbar        : {
                               xtype       : 'toolbar',
                               buttonAlign : 'right',
                               width	   : 770,
                               items       : [
                                              '-',
											   {
											    xtype   : 'button',
											    id		: 'closeButton',
											    text    : 'Close',
											    handler	: closeUploadWindow
											   },
											   '-',
                                              ]
            				}
        });      
		
		var colModel = Ext.getCmp('projectDocGrid').getColumnModel();
		colModel.setHidden(4, true);
		colModel.setHidden(6, true);
		
		win = new Ext.Window({
	    	title		: 'Upload Document',
	    	id			: 'uploadWindow',
	    	width		: 600,
	    	autoHeight	: true,
	    	border		: true,
	    	plain		: false,
	    	closable	: true,
	    	collapsible : true,
	    	animCollapse: true,
	    	draggable 	: true,
	    	maximizable : false,
	    	//layout	: 'border',
	    	onEsc		: true,
	    	model		: true,
	    	items		: [mainUploadProjectPanel]
			});
		
		
		
	    win.show(this);
	    return win;
	};
	
		function resetFileUploadForm(){
			Ext.getCmp('mainUploadProjectPanel').getForm().reset();
		}
	
		function closeUploadWindow(){
			var docGrid = Ext.getCmp('projectDocGrid');
			docGrid.store.removeAll();
			win.close();
		};
	
		function uploadedFileList(){
		var Document = Ext.data.Record.create([
			{
			    name: 'documentType'
			},
			{
			    name: 'documentName'
			}, 
			{
			    name: 'receiveDate'
			},
			{
			    name: 'fileName'
			}
		]);
		
		var form = Ext.getCmp('mainUploadProjectPanel').getForm();
		var docType = form.findField('documentType').getValue();
		var docGrid = Ext.getCmp('projectDocGrid');
		docGrid.store.add(new Document({
			documentType : form.findField('documentType').getRawValue(),
			documentName : form.findField('documentName').getValue(),
			receiveDate  : form.findField('receiveDate').getRawValue(),
			fileName	 : form.findField('file').getValue()
		}));
	};
	

    function uploadProjectDocument(projectId) {
    	var formPanel = Ext.getCmp('mainUploadProjectPanel');
    	var receiveDate = formPanel.getForm().findField('receiveDate').getValue();
   	
        var values = formPanel.getForm().getValues();
        var encodedData = Ext.encode(values); 
        formPanel.getForm().submit({
	        	   enctype	: 'multipart/form-data',	
	        	   url		: 'projectdocument/uploadDocument?projectId='+projectId+'&receiveDate='+receiveDate,
	        	   method	: 'POST', 
	        	   waitMsg	: 'Uploading Document...',
	        	   success 	: function(){
	        		   			uploadedFileList();
	        		   			alert('Document uploaded.');
       	   					 },
	        	   failure 	: function(){ 
	        		   			alert ('Process Failure');
	        		   		},
	        	   jsonData	: encodedData
       	});
    };